/**
 * 
 */
package com.yidu.drug.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.DrugMaterialsModelMapper;
import com.yidu.drug.dao.DrugModelMapper;
import com.yidu.drug.dao.MaterialsModelMapper;
import com.yidu.drug.model.DrugMaterialsModel;
import com.yidu.drug.model.DrugModel;
import com.yidu.drug.model.MaterialsModel;
import com.yidu.drug.service.DrugMaterialsService;

/**
 * 药品药品材料的Service 实现类
 * @author 王志伟
 * @date 2018年8月22日 上午9:40:47
 * @version 1.0
 */
@Service
public class DrugMaterialsServiceImpl implements DrugMaterialsService{

	//药品药品材料dao
	@Resource
	private DrugMaterialsModelMapper dmDao;
	
	//药品dao
	@Resource
	private DrugModelMapper drugDao;
	
	//材料dao
	@Resource
	private MaterialsModelMapper matDao;
	
	@Override
	public Map<String, Object> findByDrugId(String limit, String page, String drugId) {
		//NEW一个药品材料的模型,加入页面信息和药品名称
		DrugMaterialsModel dm = new DrugMaterialsModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		dm.setPage(pa);
		dm.setDrugId(drugId);
		//查询所有信息或者按条件查询
		List<DrugMaterialsModel> list=dmDao.findByDrugId(dm);
		DrugModel drug=drugDao.selectByPrimaryKey(drugId);
		for (DrugMaterialsModel drugMaterialsModel : list) {
			//加入名字
			drugMaterialsModel.setDrugName(drug.getDrugName());
			MaterialsModel mat=matDao.selectByPrimaryKey(drugMaterialsModel.getMatId());
			drugMaterialsModel.setMatName(mat.getMatName());
		}
		//查询出的信息的总行数
		Long count = dmDao.findRows(dm);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		return map;
	}

	@Override
	public int deleteByPrimaryKey(String dmId) {
		// TODO Auto-generated method stub
		return dmDao.deleteByPrimaryKey(dmId);
	}

	@Override
	public int insert(DrugMaterialsModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertSelective(DrugMaterialsModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public DrugMaterialsModel selectByPrimaryKey(String dmId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKeySelective(DrugMaterialsModel record) {
		// TODO Auto-generated method stub
		return dmDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(DrugMaterialsModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateOrAdd(String a) {
		Gson gosn=new Gson();
		//使用Type类，取得相应类型对象的class属性
		java.lang.reflect.Type listType=new TypeToken<ArrayList<DrugMaterialsModel>>(){}.getType();//TypeToken内的泛型就是Json数据中的类型
		//使用集合接受将json转换为java对象集合后的数据
		ArrayList<DrugMaterialsModel> list = gosn.fromJson(a, listType);
		int rows=0;
		for (DrugMaterialsModel drugMaterialsModel : list) {
			System.out.print("主键ID"+drugMaterialsModel.getDmId());
			System.out.print(" 药品ID"+drugMaterialsModel.getDrugId());
			System.out.print(" 材料ID"+drugMaterialsModel.getMatId());
			System.out.println(" 材料数量"+drugMaterialsModel.getDmNumber());
			System.out.println("==");
			DrugMaterialsModel dm=dmDao.selectByPrimaryKey(drugMaterialsModel.getDmId());
			if(dm==null){
				drugMaterialsModel.setDmId(Tools.getRandomUUID());
				drugMaterialsModel.setIsva(Tools.ISVA_YES);
				rows=dmDao.insertSelective(drugMaterialsModel);
			}else{
				rows=dmDao.updateByPrimaryKeySelective(drugMaterialsModel);
			}
		}
		return rows;
	}

	@Override
	public List<DrugMaterialsModel> findMatIdByDrugId(String drugId) {
		List<DrugMaterialsModel> list=dmDao.findMatIdByDrugId(drugId);
		for (DrugMaterialsModel drugMaterialsModel : list) {
			//加入名字
			MaterialsModel mat=matDao.selectByPrimaryKey(drugMaterialsModel.getMatId());
			drugMaterialsModel.setMatName(mat.getMatName());
		}
		return list;
	}

	@Override
	public int addOrDelete(String matIds, String drugId) {
		List<DrugMaterialsModel> list=dmDao.findMatIdByDrugId(drugId);
		String matId[] = matIds.split(",");
		
		
		List<DrugMaterialsModel> listOne = new ArrayList<>();
		for(int i=0; i<matId.length; i++){
			System.out.println("页面材料ID+"+matId[i]); 
			boolean flag = false;
			
			for(int j=0; j<list.size(); j++){
				System.out.println("数据库材料ID+"+list.get(j).getMatId());
				if(matId[i].equals(list.get(j).getMatId())){
					//如果相等 加入数据库的信息
					listOne.add(list.get(j));
					flag = true;
				}
			}
			if(!flag){
				//如果为false 不相等 就加入listOne集合中
				DrugMaterialsModel dm=new DrugMaterialsModel();
				dm.setDmId(Tools.getRandomUUID());
				dm.setDrugId(drugId);
				dm.setMatId(matId[i]);
				dm.setDmNumber(1L);
				dm.setIsva(Tools.ISVA_YES);
				listOne.add(dm);
			}
		}
		//先根据药品ID删除所有的药品材料配置
		dmDao.deleteByDrugId(drugId);
		int counts=0;
		for(int i=0; i<listOne.size(); i++){
			//在listOne循环加入所有的数据
			counts =dmDao.insertSelective(listOne.get(i));
		}
		return counts;
	}

}
